package abmb.pages;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import abmb.utils.MBPost;
import abmb.utils.PageParts;
import abmb.utils.Utilities;

/**
 * Servlet implementation class Followed
 * @author Andrea Balboni
 * @version 1.0
 */
public class Followed extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Followed() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Utilities utils=new abmb.utils.Utilities();
		PageParts pp=new abmb.utils.PageParts();
		HttpSession session=request.getSession(true);
		boolean loggedin=utils.isLoggedIn(session);
		ArrayList<MBPost> threadList=new ArrayList<MBPost>();
		response.setContentType("text/html");
		PrintWriter p= response.getWriter();
		p.println(pp.printHTMLHeader("AB-Microblog Home",loggedin));
		p.println(pp.printSidebar(loggedin));
		if(!loggedin){
			p.println(pp.printNoLoginMessage());
		}
		else{
			int userID=Integer.parseInt((String)session.getAttribute("userID").toString());
			threadList.clear();
			threadList=utils.getPostsByIDs(utils.findPostsIFollow(userID));
			p.println(pp.printFollowedPostTitles(threadList));
		}
		p.println(pp.printFooterSection());
		p.println(pp.printHTMLFooter());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
